package com.yzy.baselibrary.imageloader.glide

import android.content.Context
import com.yzy.baselibrary.imageloader.BaseImageLoaderStrategy
import com.yzy.baselibrary.imageloader.DiskCacheStrategyType
import com.yzy.baselibrary.imageloader.ImageConfig
import com.yzy.baselibrary.imageloader.ImageLoadScaleType
import com.yzy.baselibrary.imageloader.glide.transformations.BlurTransformation
import com.yzy.baselibrary.imageloader.glide.transformations.RoundedCornersTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions

/**
 *description: Glide实现的图片加载.
 *@date 2018/10/11 19:02.
 *@author: YinZeYu.
 */


class GlideImageLoaderStrategy : BaseImageLoaderStrategy {

  override fun loadImage(ctx: Context, config: ImageConfig) {
    if (config.url.isNullOrBlank()) {
      return
    }
    if (ctx == null) {
      return
    }
    val glideRequest = GlideApp.with(ctx).asBitmap().load(config.url)

    //缓存策略
    when (config.cacheStrategy) {
      DiskCacheStrategyType.ALL -> glideRequest.diskCacheStrategy(DiskCacheStrategy.ALL)
      DiskCacheStrategyType.NONE -> glideRequest.diskCacheStrategy(DiskCacheStrategy.NONE)
      DiskCacheStrategyType.RESOURCE -> glideRequest.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
      DiskCacheStrategyType.DATA -> glideRequest.diskCacheStrategy(DiskCacheStrategy.DATA)
      DiskCacheStrategyType.AUTOMATIC -> glideRequest.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
    }

    //ScaleType
    when (config.imageLoadScaleType) {
      ImageLoadScaleType.FitCenter -> glideRequest.fitCenter()
      ImageLoadScaleType.CenterInside -> glideRequest.centerInside()
      ImageLoadScaleType.CenterCrop -> glideRequest.centerCrop()
      ImageLoadScaleType.NoScaleType -> {
      }
    }

    //设置占位图片
    if (config.placeholder != 0) {
      glideRequest.placeholder(config.placeholder)
    }
    //设置错误的占位图片
    if (config.errorSrc != 0) {
      glideRequest.error(config.errorSrc)
    }

    //高斯模糊
    if (config.blur) {
      glideRequest.transform(BlurTransformation(config.blurRadius, config.sampling))
    }
    //圆角
    if (config.corner) {
      glideRequest.transform(RoundedCornersTransformation(config.cornerRadius, config.margin, config.cornerType))
    }

    //自定义size
    config.size?.let {
      if (it.size > 1 && it[0] > 0 && it[1] > 0) {
        glideRequest.override(it[0], it[1])
      }
    }
    //暂时只写了渐入效果，如果要多种效果可以用枚举
    if (config.useCrossFade) {
      glideRequest.transition(BitmapTransitionOptions.withCrossFade())
    } else {
      glideRequest.dontAnimate()
    }

    //缩略图
    glideRequest.thumbnail(config.thumbnail)
    config.imageView?.let {
      glideRequest.into(it)
    }

    //暂时注释掉（图片闪动）
//    glideRequest.into(object : SimpleTarget<Bitmap>() {
//      override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
//        config.imageView?.setImageBitmap(resource)
//        config.success?.invoke(resource)
//      }
//
//      override fun onLoadFailed(errorDrawable: Drawable?) {
//        super.onLoadFailed(errorDrawable)
//        config.failed?.invoke()
//      }
//    })
    //加载
//    if (config.imageView == null) {
//      if (config.success != null) {
//        glideRequest.into(object : SimpleTarget<Bitmap>() {
//          override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
//            config.success?.invoke(resource)
//          }
//
//          override fun onLoadFailed(errorDrawable: Drawable?) {
//            super.onLoadFailed(errorDrawable)
//            config.failed?.invoke()
//          }
//        })
//      }
//    } else {
    //加载回调
//      if (config.success != null || config.failed != null) {
//        glideRequest.listener(object : RequestListener<Bitmap> {
//          override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
//            if (config.failed != null) {
//              config.failed?.invoke()
//              return true
//            }
//            return false
//          }
//
//          override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
//            if (config.success != null) {
//              config.success?.invoke(resource)
//            }
//            return false
//          }
//
//        })
//      }

//    }
  }

}